<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;

use App\CustHub\Model\ChTransferRecord;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;
use function Swoole\Coroutine\Http\request;

class ChTransferRecordMapper extends AbstractMapper
{
    public function assignModel()
    {
        $this->model = ChTransferRecord::class;
    }

    public function handleSearch(Builder $query, array $params): Builder
    {

        if(!empty($params['uid'])){
            $query->where('uid',$params['uid']);
        }

        if(!empty($params['order_no'])){
            $query->where('order_no', 'like', '%'.$params['order_no'].'%');
        }

        if(!empty($params['remittance_account'])){
            $query->where('remittance_account', 'like', '%'.$params['remittance_account'].'%');
        }

        if(!empty($params['contact'])){
            $query->where('contact', 'like', '%'.$params['contact'].'%');
        }

        if(isset($params['status']) && filled($params['status'])){
            $query->where('status',$params['status']);
        }

        if (isset($params['audit_date']) && filled($params['audit_date']) && is_array($params['audit_date']) && count($params['audit_date']) == 2) {
            $query->whereBetween(
                'audit_date',
                [ $params['audit_date'][0], $params['audit_date'][1] ]
            );
        }

        if (isset($params['created_at']) && filled($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }
        return $query;
    }
}